//
//  nomalCollectionViewCell.m
//  Recycling
//
//  Created by jzd on 2018/6/4.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "nomalCollectionViewCell.h"
#import "nomalTableViewCell.h"

@interface nomalCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>

@end
@implementation nomalCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tableview.bounces = NO;
    self.tableview.delegate =self;
    self.tableview.dataSource = self;
    self.tableview.bounces = NO;
//    self.tableview.contentOffset = self.contentOff;
    [self.tableview registerNib:[UINib nibWithNibName:NSStringFromClass([nomalTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(goTop) name:@"goTop" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(goScroll:) name:@"goScroll" object:nil];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.numArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    nomalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.titleL.text = [[self.numArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]?[self.numArray objectAtIndex:indexPath.row]:[[self.numArray objectAtIndex:indexPath.row] stringValue];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableview.width, 70)];
    back.backgroundColor = [UIColor whiteColor];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, back.width, 20)];
    lab.text = [self.headerTit objectForKey:@"title"];
    lab.textColor = [UIColor darkGrayColor];
    lab.font = [UIFont systemFontOfSize:15];
    lab.textAlignment = NSTextAlignmentCenter;
    [back addSubview:lab];
    
    UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, back.width, 20)];
    price.text = [NSString stringWithFormat:@"(%@)",[self.headerTit objectForKey:@"price"]];
    price.textColor = [UIColor darkGrayColor];
    price.font = [UIFont systemFontOfSize:17];
    price.textAlignment = NSTextAlignmentCenter;
    [back addSubview:price];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, back.height-1, back.width, 1)];
    line.backgroundColor = RGBA(67, 67, 67, 0.5);
    [back addSubview:line];
    return back;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 70;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    SJLog(@"%lf====%lf",scrollView.contentOffset.y,self.contentOff.y);
//    if (!CGPointEqualToPoint(scrollView.contentOffset, self.contentOff)) {
    
        [[NSNotificationCenter defaultCenter]postNotificationName:scrollDidScNotification object:scrollView];
//    }
    
}
-(void)setNumArray:(NSArray *)numArray{
    _numArray = numArray;
    if (self.headerTit) {
        
        [self.tableview reloadData];
        
//        self.tableview.contentOffset = self.contentOff;
    }
}
-(void)setHeaderTit:(NSDictionary *)headerTit{
    _headerTit = headerTit;
    if (self.numArray) {
        
        [self.tableview reloadData];
        
//        self.tableview.contentOffset = self.contentOff;
    }
}
-(void)goScroll:(NSNotification *)noti{
    UIScrollView *scroll = (UIScrollView *)noti.object;
    [self.tableview setContentOffset:scroll.contentOffset];
}
-(void)goTop{
    [self.tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
